package sg.edu.nus.dbd.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import sg.edu.nus.dbd.biz.SQLDebugBiz;
import sg.edu.nus.dbd.formbean.SqlDebugTable;
import sg.edu.nus.dbd.util.MyLogger;

/**
 * 
 * @author Felix Halim
 * 
 */
public class SqlDebugAction extends BaseAction {

	public ActionForward performAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse res) throws Exception {
		try {
			MyLogger.debug(this.getClass(), "execute::Start...");
			String sql = req.getParameter("sql");
			req.setAttribute("sql", sql);
			req.setAttribute("results", new ArrayList<SqlDebugTable>());
			if (sql != null) {
				req.setAttribute("results", new SQLDebugBiz().executeAll(sql));
			}
			return mapping.findForward("success");
		} catch (Exception e) {
			MyLogger.error(this.getClass(), e);
			return mapping.findForward("error");
		}
	}
}
